package com.nytimes.ohos.store.sample;


import com.nytimes.ohos.store.sample.data.model.ImmutableImage;
import com.nytimes.ohos.store.sample.data.model.Post;

import com.nytimes.ohos.store.sample.util.BitmapTransform;
import com.squareup.picasso.Picasso;
import ohos.agp.components.*;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.List;
import java.util.Optional;

/**
 * @Author: Bro0cL
 * @Date: 2018/2/5 12:06
 */
public class ListAdapter extends BaseItemProvider {
    private Context mContext;
    private List<Post> mData;


    public ListAdapter(List<Post> list,  Context slice) {
        this.mData = list;
        this.mContext = slice;
    }


    public static class DefaultViewHolder {
        Text title;
        Image thumbnail;

        DefaultViewHolder(Component itemView) {
            title = (Text) itemView.findComponentById(ResourceTable.Id_title);
            thumbnail = (Image) itemView.findComponentById(ResourceTable.Id_thumbnail);
        }
    }


    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        if (mData != null && position >= 0 && position < mData.size()) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        DefaultViewHolder holder = null;
        Post data = mData.get(position);
        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_article_item, null, false);
            holder= new DefaultViewHolder(component);
            component.setTag(holder);
        } else {
            holder = (DefaultViewHolder) component.getTag();
        }
        setMaxDimensions(component);
        holder.title.setText(data.title());
        if (data.nestedThumbnail().isPresent()) {
            com.nytimes.ohos.store.sample.data.model.Image nestedImage = data.nestedThumbnail().get();
            com.nytimes.ohos.store.sample.data.model.Image image = ImmutableImage
                    .builder()
                    .height(nestedImage.height())
                    .width(nestedImage.width())
                    .url(nestedImage.url())
                    .build();
            BitmapTransform bitmapTransform = new BitmapTransform(maxWidth, maxHeight, image);

            int targetWidth = bitmapTransform.targetWidth;
            int targetHeight = bitmapTransform.targetHeight;
            holder.thumbnail.setComponentSize(targetWidth,targetHeight);
            Picasso.get()
                    .load(data.url())
                    .fit() //
                    .tag(mContext) //
                    .into(holder.thumbnail);
        }

        return component;
    }
    public static DisplayAttributes getScreenPiex(Context context){
        // 获取屏幕密度
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
        DisplayAttributes displayAttributes = display.get().getAttributes();
        //displayAttributes.xDpi;
        //displayAttributes.yDpi
        return  displayAttributes;
    }
    int maxWidth;
    int maxHeight;
    private void setMaxDimensions(Component itemView) {
        int screenWidth;
        int screenHeight;
        screenWidth = getScreenPiex(mContext).width;
        screenHeight = getScreenPiex(mContext).height;

        if (screenWidth > screenHeight) {
            screenHeight = getScreenPiex(mContext).width;
            screenWidth = getScreenPiex(mContext).height;
        }
        maxHeight = (int) (screenHeight * .55f);
        maxWidth = screenWidth - (2 * 15);
    }

}

